import { any } from "../../../core/logic";
import { Handler } from "./Handler";
import { Channel, Sampler } from "../../../component/animation/index";
import { Engine } from "../../../Engine";
class PanMouse extends Handler {
    constructor(opts = {}) {
        super(opts);
        this.button = any(opts.panButton, 0);
        this.easing = any(opts.easing, "easeOut_Cubic");
        this.dual = any(opts.dual, 1.0);//1s
        this.speed = any(opts.speed, 0.2);
        this.restrict = opts.restrict;
        this.cur = null;//当前值
        this.pre = null;
        this.register();
    }
    register() {
        this.on("mousedown", this.start);
        this.on("mousemove", this.update);
        this.on("mouseup", this.end);
        // this.on("keydown", this.fastKey);
    }
    fastKey(event) {
        if(!event.originEvent.altKey){
            return;
        } 
        this.fast(event.originEvent.keyCode);
    }
    fast(key){
        this.animation.clear();
        let { distance, roll, pitch, at } = this.target;      
        switch (key) {
            case 90://z
                if(Engine.instance.scene.defaultView){
                    let {pitch,roll,at,distance} =Engine.instance.scene.defaultView;
                    const sampler = new Sampler([0, 0.2], [this.target.pitch, pitch], "LINEAR", this.easing);
                    let channel = new Channel(this.target, "pitch");
                    channel.add(sampler);
                    this.animation.add(channel);

                    const sampler1 = new Sampler([0, 0.2], [this.target.roll, roll], "LINEAR", this.easing);
                    let channel1 = new Channel(this.target, "roll");
                    channel1.add(sampler1);
                    this.animation.add(channel1);

                    const sampler2 = new Sampler([0, 0.2], [this.target.at.toArray(), at], "LINEAR", this.easing);
                    let channel2 = new Channel(this.target, "at");
                    channel2.add(sampler2);
                    this.animation.add(channel2);

                    const sampler3 = new Sampler([0, 0.2], [this.target.distance, distance], "LINEAR", this.easing);
                    let channel3 = new Channel(this.target, "distance");
                    channel3.add(sampler3);
                    this.animation.add(channel3);
                }
                break;
            case 32://空格
                const sampler = new Sampler([0, 0.2], [this.target.pitch, 0], "LINEAR", this.easing);
                let channel = new Channel(this.target, "pitch");
                channel.add(sampler);
                this.animation.add(channel);

                const sampler1 = new Sampler([0, 0.2], [this.target.roll, 0], "LINEAR", this.easing);
                let channel1 = new Channel(this.target, "roll");
                channel1.add(sampler1);
                this.animation.add(channel1);
                break;
            case 65://a
                let cpos_65 = this.target.pixelToWorld([this.target.size[0] / 2 - 5, this.target.size[1] / 2]);
                let ppos_65 = this.target.pixelToWorld([this.target.size[0] / 2, this.target.size[1] / 2]);
                let to_65 = [at.x - (cpos_65.x - ppos_65.x), at.y - (cpos_65.y - ppos_65.y), 0];
                let sampler_65 = new Sampler([0, 0.2], [at.toArray(), to_65], "LINEAR", this.easing);
                let channel_65 = new Channel(this.target, "at");
                channel_65.add(sampler_65);
                this.animation.add(channel_65);
                break;
            case 83://s
                let cpos_68 = this.target.pixelToWorld([this.target.size[0] / 2 + 5, this.target.size[1] / 2]);
                let ppos_68 = this.target.pixelToWorld([this.target.size[0] / 2, this.target.size[1] / 2]);
                let to_68 = [at.x - (cpos_68.x - ppos_68.x), at.y - (cpos_68.y - ppos_68.y), 0];
                let sampler_68 = new Sampler([0, 0.2], [at.toArray(), to_68], "LINEAR", this.easing);
                let channel_68 = new Channel(this.target, "at");
                channel_68.add(sampler_68);
                this.animation.add(channel_68);
                break;
            case 68://d
                let cpos_87 = this.target.pixelToWorld([this.target.size[0] / 2, this.target.size[1] / 2-5]);
                let ppos_87 = this.target.pixelToWorld([this.target.size[0] / 2, this.target.size[1] / 2]);
                let to_87 = [at.x - (cpos_87.x - ppos_87.x), at.y - (cpos_87.y - ppos_87.y), 0];
                let sampler_87 = new Sampler([0, 0.2], [at.toArray(), to_87], "LINEAR", this.easing);
                let channel_87 = new Channel(this.target, "at");
                channel_87.add(sampler_87);
                this.animation.add(channel_87);
                break;
            case 70://f
                let cpos_88 = this.target.pixelToWorld([this.target.size[0] / 2, this.target.size[1] / 2+5]);
                let ppos_88 = this.target.pixelToWorld([this.target.size[0] / 2, this.target.size[1] / 2]);
                let to_88 = [at.x - (cpos_88.x - ppos_88.x), at.y - (cpos_88.y - ppos_88.y), 0];
                let sampler_88 = new Sampler([0, 0.2], [at.toArray(), to_88], "LINEAR", this.easing);
                let channel_88 = new Channel(this.target, "at");
                channel_88.add(sampler_88);
                this.animation.add(channel_88);
                break;
            case 189://-
                let sampler_189 = new Sampler([0, 0.2], [distance, distance * 1.2], "LINEAR", this.easing);
                let channel_189 = new Channel(this.target, "distance");
                channel_189.add(sampler_189);
                this.animation.add(channel_189);
                break;
            case 187://=
                let sampler_187 = new Sampler([0, 0.2], [distance, distance / 1.2], "LINEAR", this.easing);
                let channel_187 = new Channel(this.target, "distance");
                channel_187.add(sampler_187);
                this.animation.add(channel_187);
                break;
            case 71://g
                const sampler_37 = new Sampler([0, 0.2], [roll, roll - Math.PI / 36], "LINEAR", this.easing);
                let channel_37 = new Channel(this.target, "roll");
                channel_37.add(sampler_37);
                this.animation.add(channel_37);
                break;
            case 72://h
                const sampler_39 = new Sampler([0, 0.2], [roll, roll + Math.PI / 36], "LINEAR", this.easing);
                let channel_39 = new Channel(this.target, "roll");
                channel_39.add(sampler_39);
                this.animation.add(channel_39);
                break;
            case 74://j
                const sampler_38 = new Sampler([0, 0.2], [pitch, pitch + Math.PI / 36], "LINEAR", this.easing);
                let channel_38 = new Channel(this.target, "pitch");
                channel_38.add(sampler_38);
                this.animation.add(channel_38);
                break;
            case 75://k
                const sampler_40 = new Sampler([0, 0.2], [pitch, pitch - Math.PI / 36], "LINEAR", this.easing);
                let channel_40 = new Channel(this.target, "pitch");
                channel_40.add(sampler_40);
                this.animation.add(channel_40);
                break;
        }
        this.animation.start(0);
    }
    start(event) {
        if (!this.avalid) {
            return;
        }
        this.animation.stop();
        this.animation.clear();
        if (event.data.button === this.button) {
            this.alive = true;
            this.cur = this.pre = {
                pixel: event.data.pixel,
                time: event.data.time
            };
        }

    }
    update(event) {
        if(event.originEvent.buttons===0){
            return;
        }
        if (!this.alive) {
            return;
        }
        this.pre = this.cur;
        this.cur = {
            pixel: event.data.pixel,
            time: event.data.time
        }
        //计算
        let cpos = this.target.pixelToWorld([this.cur.pixel[0], this.target.size[1] / 2 + this.cur.pixel[1] - this.pre.pixel[1]]);
        let ppos = this.target.pixelToWorld([this.pre.pixel[0], this.target.size[1] / 2]);
        let to = [this.target.at.x - (cpos.x - ppos.x), this.target.at.y - (cpos.y - ppos.y)];
        if (this.restrict) {
            to[0] = Math.max(this.restrict[0], to[0]);
            to[0] = Math.min(this.restrict[2], to[0]);
            to[1] = Math.max(this.restrict[1], to[1]);
            to[1] = Math.min(this.restrict[3], to[1]);
        }
        this.target.at = to;
    }
    end(event) {
        if (!this.alive) {
            return false;
        }
        this.alive = false;
        if (this.dual <= 0) {//不执行动画
            return false;
        }
        const t = this.cur.time - this.pre.time;
        if (t == 0) {
            return false;
        }
        //v=at;s=vt-at²/2  t(已知) v(已知)  推导:s=vt-v/t*t²/2=vt/2;
        let cpos = this.target.pixelToWorld([this.cur.pixel[0], this.target.size[1] / 2 + this.cur.pixel[1] - this.pre.pixel[1]]);
        let ppos = this.target.pixelToWorld([this.pre.pixel[0], this.target.size[1] / 2]);
        const diff = [cpos.x - ppos.x, cpos.y - ppos.y];
        const v = [diff[0] / t, diff[1] / t];
        const s = [v[0] * this.dual * this.speed / 2, v[1] * this.dual * this.speed / 2];
        let to = [this.target.at.x - s[0], this.target.at.y - s[1], this.target.at.z];
        if (this.restrict) {
            to[0] = Math.max(this.restrict[0], to[0]);
            to[0] = Math.min(this.restrict[2], to[0]);
            to[1] = Math.max(this.restrict[1], to[1]);
            to[1] = Math.min(this.restrict[3], to[1]);
        }
        const sampler = new Sampler([0, this.dual], [this.target.at.toArray(), to], "LINEAR", this.easing);
        let channel = new Channel(this.target, "at");
        channel.add(sampler);
        this.animation.add(channel);
        this.animation.start(0);
    }
}
export { PanMouse }